package com.pocket.insight.controller;

import com.pocket.insight.base.dto.Result;
import com.pocket.insight.entity.App;
import com.pocket.insight.service.AppService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用管理控制器
 *
 * @author shuize
 */
@Tag(name = "应用管理")
@RestController
@RequestMapping("/app")
public class AppController {

    @Resource
    private AppService appService;

    @PostMapping
    @Operation(summary = "新增应用")
    public Result<App> save(@RequestBody App app) {
        return Result.success(appService.save(app));
    }

    @GetMapping
    @Operation(summary = "应用列表")
    public Result<List<App>> list() {
        return Result.success(appService.list());
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除应用")
    public Result<Void> delete(@PathVariable Long id) {
        appService.delete(id);
        return Result.success();
    }

    @GetMapping("{id}")
    @Operation(summary = "查看应用详情")
    public Result<App> detail(@PathVariable Long id) {
        return Result.success(appService.getById(id));
    }

    @PutMapping("{id}")
    @Operation(summary = "更新应用")
    public Result<App> update(@PathVariable Long id, @RequestBody App app) {
        app.setId(id);
        return Result.success(appService.update(app));
    }

    @PostMapping("{id}/publish")
    @Operation(summary = "发布应用")
    public Result<App> publish(@PathVariable Long id) {
        return Result.success(appService.publish(id));
    }

    @PostMapping("{id}/unpublish") 
    @Operation(summary = "取消发布应用")
    public Result<App> unpublish(@PathVariable Long id) {
        return Result.success(appService.unpublish(id));
    }
}
